package com.nhutcm.trackingthief.utils;

import android.content.Context;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.nhutcm.trackingthief.R;
import com.nhutcm.trackingthief.receiver.SecretCodeReceiver;

public class ReceiverUtils {

	public static void registerSecretReceiver(Context context, String defaultCode) {

		SharedPreferences sharedPrefs = PreferenceManager
				.getDefaultSharedPreferences(context);

		String strTime = String.valueOf(System.currentTimeMillis());

		String code = sharedPrefs.getString(
				context.getString(R.string.pref_code_key), strTime);
		if (code.equals(strTime)) {
			code = defaultCode;
			
			PrefUtils.saveStringToPreference(context,
					context.getString(R.string.pref_code_key),
					code);
		}

		IntentFilter i = new IntentFilter(
				"android.provider.Telephony.SECRET_CODE");
		i.addDataScheme("android_secret_code");
		i.addDataAuthority(code, null);

		SecretCodeReceiver receiver = new SecretCodeReceiver();
		context.registerReceiver(receiver, i);
		
		Constant.currentReceiver = receiver;

	}
	
}
